import * as vscode from 'vscode';

class Logger {
    private static readonly tag: string = "Auto Tools";

    // 解析调用栈，获取调用者的文件名和行号
    private static GetCallerInfo(): { fileName: string; line: number; column: number } {
        // 创建一个Error对象，用于获取调用栈
        const error = new Error();
        // 解析stack（不同环境格式可能略有差异，这里适配Node.js）
        const stackLines = error.stack?.split('\n') || [];

        // stackLines结构大致为：
        // [0] 'Error'
        // [1] '    at Logger.getCallerInfo (filePath:line:column)'  // 当前方法自身
        // [2] '    at Logger.info (filePath:line:column)'          // 日志方法自身
        // [3] '    at Object.<anonymous> (callerFilePath:line:column)'  // 实际调用者
        // 因此取第3行（索引2）作为调用者信息
        const callerLine = stackLines[3] || '';

        // 正则匹配文件名、行号、列号（适配Node.js的stack格式）
        const match = callerLine.match(/\((.*):(\d+):(\d+)\)/);
        if (!match) {
            return { fileName: 'unknown', line: 0, column: 0 };
        }

        // 提取文件名（简化为仅文件名，去掉路径）
        const fullPath = match[1];
        const fileName = fullPath.split(/[\\/]/).pop() || fullPath;

        return {
            fileName,
            line: Number(match[2]),
            column: Number(match[3]),
        };
    }

    private static GetCurrentTime(): string {
        const date = new Date();
        return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`;
    }
    public static Error(message?: any, ...optionalParams: any[]) {
        const callerInfo = Logger.GetCallerInfo();
        console.error(`[${Logger.GetCurrentTime()}][E][${Logger.tag}][${callerInfo.fileName}:${callerInfo.line}:${callerInfo.column}]: `, message, optionalParams);
    }
    public static Warn(message?: any, ...optionalParams: any[]) {
        const callerInfo = Logger.GetCallerInfo();
        console.warn(`[${Logger.GetCurrentTime()}][W][${Logger.tag}][${callerInfo.fileName}:${callerInfo.line}:${callerInfo.column}]: `, message, optionalParams);
    }
    
    public static Info(message?: any, ...optionalParams: any[]) {
        const callerInfo = Logger.GetCallerInfo();
        console.info(`[${Logger.GetCurrentTime()}][I][${Logger.tag}][${callerInfo.fileName}:${callerInfo.line}:${callerInfo.column}]: `, message, optionalParams);
    }
    public static Debug(message?: any, ...optionalParams: any[]) {
        const callerInfo = Logger.GetCallerInfo();
        console.debug(`[${Logger.GetCurrentTime()}][D][${Logger.tag}][${callerInfo.fileName}:${callerInfo.line}:${callerInfo.column}]: `, message, optionalParams);
    }
    public static Message(message: string, enableCaller: boolean = true): void {
        if (enableCaller) {
            const callerInfo = Logger.GetCallerInfo();
            vscode.window.showInformationMessage(`[${Logger.GetCurrentTime()}][D][${Logger.tag}][${callerInfo.fileName}:${callerInfo.line}:${callerInfo.column}]: ${message}`);
        } else {
            vscode.window.showInformationMessage(message);
        }
    }
}

export default Logger;